package com.ffcs.cn.action.desktopcontrol.smComboBoxFieldInfo;

import com.supermap.data.Dataset;
import com.supermap.data.DatasetVector;
import com.supermap.data.Datasource;
import com.supermap.data.FieldType;
import com.supermap.desktop.controls.ui.controls.SmDialog;
import com.supermap.desktop.controls.ui.controls.comboBox.SmComboBoxDataset;
import com.supermap.desktop.controls.ui.controls.comboBox.SmComboBoxDatasource;
import com.supermap.desktop.controls.ui.controls.comboBox.SmComboBoxFieldInfo;
import com.supermap.desktop.core.ui.controls.GridBagConstraintsHelper;
import com.supermap.desktop.core.utilties.DatasetTypeUtilities;
import com.supermap.desktop.core.utilties.DefaultValues;
import com.supermap.desktop.core.utilties.FieldTypeUtilities;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**

 */
public class DialogSmComboBoxFieldInfo extends SmDialog {
	private SmComboBoxDatasource comboBoxDatasource;
	private SmComboBoxDataset comboBoxDataset;
	private SmComboBoxFieldInfo comboBoxFieldInfo;

	//设置第一项是否为空值
	private JCheckBox checkBoxFirstItemEmpty;

	//支持显示的字段类型
	private JLabel labelSupportFieldTypes;
	private JList<String> listSupportFieldTypes;

	public DialogSmComboBoxFieldInfo() {
		setTitle("SmComboBoxFieldInfo 示例");
		setSize(490,390);
		initComponents();
		initLayout();
		initListener();
	}

	private void initListener() {
		//设置第一项是否为空值
		checkBoxFirstItemEmpty.addActionListener(e -> comboBoxFieldInfo.setFirstItemEmpty(checkBoxFirstItemEmpty.isSelected()));
		//设置支持显示的字段类型
		listSupportFieldTypes.addListSelectionListener(e -> {
			List<String> list = listSupportFieldTypes.getSelectedValuesList();
			if (list == null || list.isEmpty()) {
				return;
			}
			FieldType[] fieldTypes = list.stream().map(FieldTypeUtilities::getFieldType).toArray(FieldType[]::new);
			comboBoxFieldInfo.setSupportFieldTypes(fieldTypes);
		});
		//设置选中数据源
		comboBoxDatasource.addItemListener(e -> {
			Datasource datasource = comboBoxDatasource.getSelectedDatasource();
			Optional.ofNullable(datasource).ifPresent(comboBoxDataset::setDatasource);
		});
		//设置选中数据集
		comboBoxDataset.addItemListener(e -> {
			Dataset selectedDataset = comboBoxDataset.getSelectedDataset();
			Optional.ofNullable(selectedDataset)
					.filter(dataset -> dataset instanceof DatasetVector)
					.map(dataset -> (DatasetVector) dataset)
					.ifPresent(comboBoxFieldInfo::setDatasetVector);
		});
	}

	private void initLayout() {
		this.setLayout(new GridBagLayout());
		this.add(getLeftPanel(), new GridBagConstraintsHelper(0, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.CONTROLS_GAP)
				.setWeight(1, 1)
				.setFill(GridBagConstraints.BOTH));

		this.add(getRightPanel(), new GridBagConstraintsHelper(1, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, 0,
						GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setWeight(1, 1)
				.setFill(GridBagConstraints.BOTH));
	}

	private Component getRightPanel() {
		JPanel panel = new JPanel();
		panel.setLayout(new GridBagLayout());
		panel.setBorder(new TitledBorder("控制面板"));

		panel.add(new JLabel("数据源:"), new GridBagConstraintsHelper(0, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, 0, 0)
				.setAnchor(GridBagConstraints.WEST));
		panel.add(comboBoxDatasource, new GridBagConstraintsHelper(1, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.CONTROLS_GAP,
						0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST).setWeight(1,0).setFill(GridBagConstraints.HORIZONTAL));

		panel.add(new JLabel("数据集:"), new GridBagConstraintsHelper(0, 1, 1, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, 0, 0)
				.setAnchor(GridBagConstraints.WEST));
		panel.add(comboBoxDataset, new GridBagConstraintsHelper(1, 1, 1, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.CONTROLS_GAP,
						0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST).setWeight(1,0).setFill(GridBagConstraints.HORIZONTAL));


		panel.add(checkBoxFirstItemEmpty, new GridBagConstraintsHelper(0, 2, 2, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST));

		panel.add(labelSupportFieldTypes, new GridBagConstraintsHelper(0, 3, 2, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST));
		JScrollPane pane = new JScrollPane(listSupportFieldTypes);
		pane.setPreferredSize(new Dimension(100,100));
		panel.add(pane, new GridBagConstraintsHelper(0, 4, 2, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST).setWeight(1,1).setFill(GridBagConstraints.BOTH));
		return panel;
	}

	private Component getLeftPanel() {
		JPanel panel = new JPanel();
		panel.setLayout(new GridBagLayout());
		panel.setBorder(new TitledBorder("控件"));
		comboBoxFieldInfo.setPreferredSize(new Dimension(100,DefaultValues.getDefaultComponentHeight()));
		panel.add(new JLabel("数据集字段:"), new GridBagConstraintsHelper(0, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, 0, 0)
				.setAnchor(GridBagConstraints.WEST));
		panel.add(comboBoxFieldInfo, new GridBagConstraintsHelper(1, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST).setWeight(1, 1).setFill(GridBagConstraints.HORIZONTAL));
		return panel;
	}

	private void initComponents() {
		comboBoxDatasource = new SmComboBoxDatasource();
		comboBoxDatasource.setSupportOpen(true);
		comboBoxDataset = new SmComboBoxDataset();
		comboBoxDataset.setSupportedDatasetTypes(DatasetTypeUtilities.getDatasetTypeVector());
		comboBoxFieldInfo = new SmComboBoxFieldInfo();

		Datasource selectedDatasource = comboBoxDatasource.getSelectedDatasource();
		Optional.ofNullable(selectedDatasource)
				.ifPresent(comboBoxDataset::setDatasource);

		Dataset selectedDataset = comboBoxDataset.getSelectedDataset();
		Optional.ofNullable(selectedDataset)
				.filter(dataset -> dataset instanceof DatasetVector)
				.map(dataset -> (DatasetVector) dataset)
				.ifPresent(comboBoxFieldInfo::setDatasetVector);

		checkBoxFirstItemEmpty = new JCheckBox("显示第一项为空值");
		checkBoxFirstItemEmpty.setSelected(comboBoxFieldInfo.isFirstItemEmpty());

		labelSupportFieldTypes = new JLabel("支持显示的字段类型：");
		listSupportFieldTypes = new JList<>();
		FieldType[] allSupportType = FieldTypeUtilities.getAllSupportType();
		String[] types = Arrays.stream(allSupportType).map(FieldTypeUtilities::getFieldTypeName).toArray(String[]::new);
		listSupportFieldTypes.setListData(types);
	}
}
